package com.scraper.articles.web.controller;

import com.scraper.articles.web.model.Request;
import com.scraper.articles.web.scrapers.RequestsProcessor;
import com.scraper.articles.web.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/run")
public class RunController {

	@Autowired
	private RequestService requestService;

	@Autowired
	private RequestsProcessor requestsProcessor;


	@RequestMapping(value = { "/", "/runRequests" })
	public String runRequests() throws IOException {

		List<Request> requestList = requestService.listRequests();
		requestsProcessor.process(requestList);

		return "redirect:../../request/";
	}

	@RequestMapping("/runRequest/{requestId}")
	public String runRequest(@PathVariable Long requestId) {

		Request request = requestService.getRequest(requestId);
		requestsProcessor.process(Arrays.asList(request));

		return "redirect:../../request/";
	}
}
